package com.laoguo.service;

import com.laoguo.anno.DataSource;
import com.laoguo.config.CustomerContextHolder;
import com.laoguo.dao.UserMapper;
import com.laoguo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 通过ID查找用户
     * @param id
     * @return
     */
    public void findById(Integer id) {
        System.out.println("第一个查询:" + CustomerContextHolder.getCustomerType());
        User user = userMapper.selectById(id);
        System.out.println("user:" + user.toString());
    }

    /**
     * 通过ID查找用户
     * @param id
     * @return
     */
    @DataSource(name = CustomerContextHolder.DATA_SOURCE_SLAVE)
    public void findById1(Integer id) {
        System.out.println("第二个查询，从数据库:" + CustomerContextHolder.getCustomerType());
        User user = userMapper.selectById(id);
        System.out.println("user:" + user.toString());
    }
}
